//
//  TianmuNewSuspensionViewController.m
//  TianmuSDK-Demo
//
//  Created by 陈则富 on 2021/9/13.
//

#import "TianmuNewSuspensionViewController.h"
#import <ADSuyiKit/ADSuyiKitMacros.h>
#import <TianmuSDK/TianmuSuspensionAdView.h>
#import "UIView+Toast.h"


@interface TianmuNewSuspensionViewController()<TianmuSuspensionAdViewDelegate> {
    BOOL _isHeadBidding;
    BOOL _isSucceed;
    BOOL _isFail;
}

@property (nonatomic, strong) NSMutableArray<UIButton *> *btns;

@property (nonatomic, assign) TianmuSuspensionType suspensionType;

@property (nonatomic ,strong) TianmuSuspensionAdView *suspensionAdView;

@end

@implementation TianmuNewSuspensionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.suspensionType = TianmuSuspensionTypeProfile;
    
    UIButton *setAdConfigBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [setAdConfigBtn setTitle:@"切换" forState:(UIControlStateNormal)];
    [setAdConfigBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    setAdConfigBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    setAdConfigBtn.frame = CGRectMake(0, 0, 50, 20);
    [setAdConfigBtn addTarget:self action:@selector(showTypeSelect) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:setAdConfigBtn];
    self.navigationItem.rightBarButtonItem = rightItem;

    CGFloat margin = self.view.bounds.size.width/5 - 320/5;
    
    UIButton *loadAndShowBtn = [UIButton new];
    [loadAndShowBtn setTitle:@"普通请求" forState:(UIControlStateNormal)];
    [loadAndShowBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    loadAndShowBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    loadAndShowBtn.backgroundColor = UIColor.lightGrayColor;
    loadAndShowBtn.clipsToBounds = YES;
    loadAndShowBtn.layer.cornerRadius = 3;
    [loadAndShowBtn addTarget:self action:@selector(loadNomarlAd) forControlEvents:(UIControlEventTouchUpInside)];
    loadAndShowBtn.frame = CGRectMake(margin, self.view.bounds.size.height - 60, 80, 30);
    [self.view addSubview:loadAndShowBtn];
    [self.view bringSubviewToFront:loadAndShowBtn];
    
    UIButton *loadBtn = [UIButton new];
    [loadBtn setTitle:@"竞价请求" forState:(UIControlStateNormal)];
    [loadBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    loadBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    loadBtn.backgroundColor = UIColor.lightGrayColor;
    loadBtn.clipsToBounds = YES;
    loadBtn.layer.cornerRadius = 3;
    [loadBtn addTarget:self action:@selector(loadBidAd) forControlEvents:(UIControlEventTouchUpInside)];
    loadBtn.frame = CGRectMake(margin*2 + 80, self.view.bounds.size.height - 60, 80, 30);
    [self.view addSubview:loadBtn];
    [self.view bringSubviewToFront:loadBtn];
    
    UIButton *bidWinBtn = [UIButton new];
    [bidWinBtn setTitle:@"竞价成功" forState:(UIControlStateNormal)];
    [bidWinBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    bidWinBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    bidWinBtn.backgroundColor = UIColor.lightGrayColor;
    bidWinBtn.clipsToBounds = YES;
    bidWinBtn.layer.cornerRadius = 3;
    [bidWinBtn addTarget:self action:@selector(bidWin) forControlEvents:(UIControlEventTouchUpInside)];
    bidWinBtn.frame = CGRectMake(160 + margin*3, self.view.bounds.size.height - 60, 80, 30);
    [self.view addSubview:bidWinBtn];
    [self.view bringSubviewToFront:bidWinBtn];
    
    UIButton *bidFailBtn = [UIButton new];
    [bidFailBtn setTitle:@"竞价失败" forState:(UIControlStateNormal)];
    [bidFailBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    bidFailBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    bidFailBtn.backgroundColor = UIColor.lightGrayColor;
    bidFailBtn.clipsToBounds = YES;
    bidFailBtn.layer.cornerRadius = 3;
    [bidFailBtn addTarget:self action:@selector(bidFail) forControlEvents:(UIControlEventTouchUpInside)];
    bidFailBtn.frame = CGRectMake(margin*4 + 240, self.view.bounds.size.height - 60, 80, 30);
    [self.view addSubview:bidFailBtn];
    [self.view bringSubviewToFront:bidFailBtn];
}

- (void)loadNomarlAd{
    _isHeadBidding = NO;
    _isSucceed = NO;
    [self loadSuspension:@"af87d509263e"];
}

- (void)loadBidAd{
    _isHeadBidding = YES;
    _isSucceed = NO;
    [self loadSuspension:@"8cf476d53201"];
}

- (void)bidWin{
    if (!_isHeadBidding) {
        [self.view makeToast:@"当前广告不是竞价广告"];
        return;
    }
    if (!_isSucceed || !_suspensionAdView) {
        [self.view makeToast:[NSString stringWithFormat:@"横幅广告未加载成功"]];
        return;
    }
    if (_isFail) {
        [self.view makeToast:[NSString stringWithFormat:@"横幅广告已竞败"]];
        return;
    }
    // 发送竞价成功通知
    // 如天目从竞价队列中胜出，则传入广告返回的出价，如：[adView bidPrice]（单位：分）
    [self.suspensionAdView sendWinNotificationWithPrice:[self.suspensionAdView bidPrice]];
    [self.view addSubview:self.suspensionAdView];
}

- (void)bidFail{
    _isFail = YES;
    if (!_isHeadBidding) {
        [self.view makeToast:@"当前广告不是竞价广告"];
        return;
    }
    if (!_isSucceed || !_suspensionAdView) {
        [self.view makeToast:[NSString stringWithFormat:@"横幅广告未加载成功"]];
        return;
    }
    [self.suspensionAdView sendWinFailNotificationReason:TianmuAdBiddingLossReasonLowPrice winnerPirce:100];
}

- (void)loadSuspension:(NSString *)posId {
    _isFail = NO;
    [self.suspensionAdView removeFromSuperview];
    self.suspensionAdView = nil;
    
    CGSize adSize;
    if (_suspensionType == TianmuSuspensionTypeProfile) {
        adSize = CGSizeMake(243, 28);
    } else if (_suspensionType == TianmuSuspensionTypeSingleText) {
        adSize = CGSizeMake(260, 34);
    } else {// _suspensionType == TianmuSuspensionTypeMultipleText
        adSize = CGSizeMake(83, 127);
    }
    self.suspensionAdView = [[TianmuSuspensionAdView alloc] initWithFrame:CGRectMake((kADSYScreenWidth-adSize.width)/2, 250, adSize.width, adSize.height) posId:posId suspensionType:self.suspensionType];
    self.suspensionAdView.delegate = self;
    self.suspensionAdView.isAutoPlay = NO;
    self.suspensionAdView.viewController = self;
    [self.suspensionAdView loadRequest];
}

#pragma mark - TianmuSuspensionAdViewDelegate

/**
 *  请求成功
 */
- (void)tianmuSuspensionSuccessLoad:(TianmuSuspensionAdView *)tianmuSuspensionView {
    NSLog(@"%s，创新广告加载成功",__func__);
    _isSucceed = YES;
    if (_isHeadBidding) {
        [_suspensionAdView removeFromSuperview];
        [self.view makeToast:[NSString stringWithFormat:@"询价成功：%ld",[tianmuSuspensionView bidPrice]]];
    } else {
        [self.view addSubview:self.suspensionAdView];
    }
}

/**
 *  请求失败
 */
- (void)tianmuSuspensionViewFailedToLoadWithError:(NSError *)error {
    NSLog(@"创新广告加载失败%@",error);
}

/**
 *  展示回调
 */
- (void)tianmuSuspensionViewDidPresent:(TianmuSuspensionAdView *)tianmuSuspensionView{
    NSLog(@"%s，创新广告展示成功",__func__);
}

/**
 *  曝光回调
 */
- (void)tianmuSuspensionViewWillExpose:(TianmuSuspensionAdView *)tianmuSuspensionView{
    NSLog(@"%s，创新广告曝光",__func__);
}


/**
 *  点击回调
 */
- (void)tianmuSuspensionViewClicked:(TianmuSuspensionAdView *)tianmuSuspensionView{
    NSLog(@"%s，创新广告点击",__func__);
}


/**
 *  被用户关闭时调用
 */
- (void)tianmuSuspensionViewWillClose:(TianmuSuspensionAdView *)tianmuSuspensionView{
    NSLog(@"%s，创新广告关闭",__func__);
    [_suspensionAdView removeFromSuperview];
    _suspensionAdView = nil;
}

/**
 *  被用户关闭广告落地页调用
 */
- (void)tianmuSuspensionViewCloseLandingPage:(TianmuSuspensionAdView *)tianmuSuspensionView{
    NSLog(@"%s，创新广告关闭落地页",__func__);
}

#pragma mark - other

- (NSMutableArray<UIButton *> *)btns {
    if(!_btns) {
        _btns = [NSMutableArray new];
    }
    return _btns;;
}

- (void)showTypeSelect {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"" message:@"选择创新广告类型" preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *profileType = [UIAlertAction actionWithTitle:@"头像广告" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.suspensionType = TianmuSuspensionTypeProfile;
        [self loadSuspension:@"af87d509263e"];
    }];
    UIAlertAction *singleTextType = [UIAlertAction actionWithTitle:@"单行文字链广告" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.suspensionType = TianmuSuspensionTypeSingleText;
        [self loadSuspension:@"af87d509263e"];
    }];
    UIAlertAction *multipleTextType = [UIAlertAction actionWithTitle:@"多行文字链广告" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.suspensionType = TianmuSuspensionTypeMultipleText;
        [self loadSuspension:@"af87d509263e"];
    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    [alertVc addAction:profileType];
    [alertVc addAction:singleTextType];
    [alertVc addAction:multipleTextType];
    [alertVc addAction:cancle];
    [self presentViewController:alertVc animated:YES completion:nil];
}

@end
